package com.powernode.controller;

import cn.hutool.core.util.ArrayUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Basket;
import com.powernode.model.CartVo;
import com.powernode.service.BasketService;
import com.powernode.utils.AuthUtil;
import com.powernode.vo.CartMoneyVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 内容:
 * 作者: cy
 */

@RestController
@Api(tags = "购物车管理接口")
@RequestMapping("p/shopCart")
public class CartController {

   @Autowired
   private BasketService basketService;

   @GetMapping("prodCount")
   @ApiOperation("查询购物车中商品的数量")
   public ResponseEntity<Integer> getBasketCount() {
      String openId = AuthUtil.getOpenId();
      Integer count = basketService.getBasketCount(openId);
      return ResponseEntity.ok(count);
   }

   @PostMapping("changeItem")
   @ApiOperation("修改购物车中商品的数量(添加/修改)")
   public ResponseEntity<Void> changeItem(@RequestBody Basket basket) {
      String openId = AuthUtil.getOpenId();
      basketService.changeItem(openId, basket);
      return ResponseEntity.ok().build();
   }
   @GetMapping("info")
   @ApiOperation("查询购物车中商品的信息")
   public ResponseEntity<CartVo> getCartInfo(){
      String openId = AuthUtil.getOpenId();
      CartVo cartVo = basketService.getCartInfo(openId);
      return ResponseEntity.ok(cartVo);

   }
   @DeleteMapping("deleteItem")
   @ApiOperation("删除购物车中选中的商品")
   public ResponseEntity<Void> deleteItem(@RequestBody List<Long> basketIds) {
      basketService.removeByIds(basketIds);
      return ResponseEntity.ok().build();
   }

   @PostMapping("totalPay")
   @ApiOperation("计算购物车中选中商品的总价")
   public ResponseEntity<CartMoneyVo> calculateTotalPay(@RequestBody List<Long> basketIds) {
      CartMoneyVo cartMoneyVo = basketService.calculateTotalPay(basketIds);
      return ResponseEntity.ok(cartMoneyVo);
   }

   @GetMapping("getBasketsByIds")
   @ApiOperation("根据购物车ids查询购物车集合")
   List<Basket> getBasketsByIds(@RequestParam List<Long> basketIds) {
      return basketService.listByIds(basketIds);
   }
   @PostMapping("clearCart")
   @ApiOperation("根据会员的id和skuIds清空购物车")
   Boolean clearCart(@RequestBody List<Long> skuIds, @RequestParam String openId) {
      return basketService.remove(new LambdaQueryWrapper<Basket>()
              .eq(Basket::getUserId, openId)
              .in(Basket::getSkuId, skuIds)
      );
   }



}
